package com.gongpingjialoan.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.utility.StepMonitor;

/**
 * Created by Administrator on 2016/10/26.
 */
public class ContactPopup implements View.OnClickListener {

    private Context context;

    private View contentV, mainView, lineView;

    private PopupWindow pop;

    private ContactReslutClickListener mContactReslutClickListener;

    private TextView onLineTextView, phoneTextView, orderTextView;

    private boolean isAppiont, haveChatId;

    boolean showVipContact = false;

    View vip_contactV;


    public ContactPopup(Context context, ContactReslutClickListener mContactReslutClickListener, boolean isAppiont, boolean haveChatId) {
        this.context = context;
        this.isAppiont = isAppiont;
        this.haveChatId = haveChatId;
        this.mContactReslutClickListener = mContactReslutClickListener;
        contentV = LayoutInflater.from(context)
                .inflate(R.layout.contact_layout_item, null);
        pop = new PopupWindow(contentV, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        // pop.setAnimationStyle(R.style.mystyle);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        initView();
    }


    public ContactPopup(Context context, ContactReslutClickListener mContactReslutClickListener, boolean isAppiont, boolean haveChatId, boolean showVipContact) {
        this.context = context;
        this.isAppiont = isAppiont;
        this.haveChatId = haveChatId;
        this.showVipContact = showVipContact;
        this.mContactReslutClickListener = mContactReslutClickListener;
        contentV = LayoutInflater.from(context)
                .inflate(R.layout.contact_layout_item, null);
        pop = new PopupWindow(contentV, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);
        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        // pop.setAnimationStyle(R.style.mystyle);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        initView();
    }

    public void setOrderTextView() {
        if (orderTextView != null) {
            orderTextView.setText("已预约");
        }
    }

    public void setIsAppiont(boolean isAppiont) {
        this.isAppiont = isAppiont;
    }

    private void initView() {
        mainView = contentV.findViewById(R.id.main);
        lineView = contentV.findViewById(R.id.line);
        onLineTextView = (TextView) contentV.findViewById(R.id.online);
        phoneTextView = (TextView) contentV.findViewById(R.id.phone);
        orderTextView = (TextView) contentV.findViewById(R.id.order);
        if (isAppiont) {
            orderTextView.setText("已预约");
        }
        onLineTextView.setOnClickListener(this);
        phoneTextView.setOnClickListener(this);
        orderTextView.setOnClickListener(this);
        mainView.setOnClickListener(this);
        if (!haveChatId) {
            hideOnline();
        }
        vip_contactV = contentV.findViewById(R.id.vip_contact);
        vip_contactV.setOnClickListener(this);
        if (showVipContact) {
            contentV.findViewById(R.id.vip_line).setVisibility(View.VISIBLE);
            vip_contactV.setVisibility(View.VISIBLE);
        }


    }


    public void hideOnline() {
        if (onLineTextView != null) {
            onLineTextView.setVisibility(View.GONE);
        }
        if (lineView != null) {
            lineView.setVisibility(View.GONE);
        }
    }

    public void show(View v) {
        pop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View v) {
        if (v == onLineTextView) {
            PointRecordManage.getInstance().addPoint(context, Piont.SourceVIPService);
            if (mContactReslutClickListener != null) {
                pop.dismiss();
                StepMonitor.getInstance().addMonitor(new Monitor("click", "detail_chatOnline", System.currentTimeMillis(), 1f));
                mContactReslutClickListener.online();
            }
        } else if (v == phoneTextView) {
            PointRecordManage.getInstance().addPoint(context, Piont.SourceMobile);
            if (mContactReslutClickListener != null) {
                pop.dismiss();

                mContactReslutClickListener.phone();
            }
        } else if (v == orderTextView) {
            PointRecordManage.getInstance().addPoint(context, Piont.SourceAppointment);
            if (mContactReslutClickListener != null) {
                pop.dismiss();
                mContactReslutClickListener.order();
            }
        } else if (v == mainView) {
            pop.dismiss();
        } else if (v == vip_contactV) {
            if (mContactReslutClickListener != null) {
                pop.dismiss();
                mContactReslutClickListener.onVipContact();
            }
        }
    }


    public interface ContactReslutClickListener {

        void phone();

        void order();

        void online();

        void onVipContact();
    }
}
